package org.wikipedia;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author kalle
* @since 2013-07-29 13:17
*/
public class WikiBot extends Wiki {
private String botName;
private String usernameLoggedInToWiki;
private String botHomePageTitle;
private String botLogPageTitle;
public WikiBot() {
}
public WikiBot(String domain) {
super(domain);
}
public WikiBot(String domain, String scriptPath) {
super(domain, scriptPath);
}
@Override
protected void initVars() {
super.initVars();
setMarkBot(true);
}
@Override
public String getPageText(String title) throws IOException {
if (!exists(title)[0]) {
return null;
} else {
return super.getPageText(title);
}
}
@Override
public synchronized void login(String username, char[] password) throws IOException, FailedLoginException {
super.login(username, password);
usernameLoggedInToWiki = username;
botHomePageTitle = "User:" + usernameLoggedInToWiki + "/bots/" + getBotName();
botLogPageTitle = botHomePageTitle + "/log";
try {
initializeBotPages();
} catch (LoginException e) {
throw new RuntimeException("Could not initialize Wiki pages to be associated with bot", e);
}
}
public void addToBotWikiLog(String message, Exception e) throws IOException, LoginException {
StringWriter writer = new StringWriter();
writer.append(message).append("<br/>\n");
writer.append("<pre>");
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
printWriter.flush();
writer.append("</pre>");
addToBotWikiLog(writer.toString());
}
public void addToBotWikiLog(String message) throws IOException, LoginException {
String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String logPageText = getPageText(botLogPageTitle);
logPageText = now + " " + message + "<br/>\n" + logPageText;
edit(botLogPageTitle, logPageText, "new log entry");
}
public String getUsernameLoggedInToWiki() {
return usernameLoggedInToWiki;
}
public String getBotHomePageTitle() {
return botHomePageTitle;
}
public String getBotLogPageTitle() {
return botLogPageTitle;
}
public String getBotName() {
return botName;
}
public void setBotName(String botName) {
this.botName = botName;
}
public void initializeBotPages() throws IOException, LoginException {
if (!exists(botHomePageTitle)[0]) {
StringBuilder out = new StringBuilder(4096);
out.append("This is the home page of bot ").append(botName).append(", owned by [[User:").append(usernameLoggedInToWiki).append("]]\n");
out.append("\n");
out.append("See [[").append(botHomePageTitle).append("/log]] for what I've been up to.\n");
edit(botHomePageTitle, out.toString(), "created default bot home page");
}
if (!exists(botLogPageTitle)[0]) {
StringBuilder out = new StringBuilder(4096);
out.append("<br/><br/>\n\nThis is the log page of bot ").append(botName).append(", owned by [[User:").append(usernameLoggedInToWiki).append("]]\n");
edit(botLogPageTitle, out.toString(), "created default bot log page");
}
}
}